package com.inool.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inool.common.exception.InoolException;
import com.inool.common.login.LoginUser;
import com.inool.common.login.LoginUserHolder;
import com.inool.common.result.Result;
import com.inool.model.Course;
import com.inool.service.CourseService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "课程管理")
@RestController
@RequestMapping("/api/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @PostMapping("add")
    @Operation(summary = "增加课程")
    public Result<Course> addCourse(@RequestBody Course course) {
        Course newCourse = courseService.addCourse(course);
        return Result.ok(newCourse);
    }

    @Operation(summary = "更新课程")
    @PostMapping("update/{id}")
    public Result<Course> updateCourse(@PathVariable String id, @RequestBody Course course) {
        Course updatedCourse = courseService.updateCourse(id, course);
        if (updatedCourse != null) {
            return Result.ok(updatedCourse);
        }
        return Result.ok();
    }

    @Operation(summary = "删除课程")
    @PostMapping("remove/{id}")
    public Result<Void> deleteCourse(@PathVariable String id) {
        courseService.deleteCourse(id);
        return Result.ok();
    }

    @Operation(summary = "根据课程Id获取课程")
    @PostMapping("get/{id}")
    public Result<Course> getCourseById(@PathVariable String id) {
        Course course = courseService.getCourseById(id);
        if (course != null) {
            return Result.ok(course);
        }
        return Result.ok(course);
    }

    @Operation(summary = "获取全部课程")
    @GetMapping("all")
    public Result<List<Course>> getAllCourses() {
        List<Course> courses = courseService.getAllCourses();
        return Result.ok(courses);
    }

    @Operation(summary = "分页查询课程")
    @GetMapping("page")
    public Result<Page<Course>> pageCourses(@RequestParam long current, @RequestParam long size,
                                            String courseName, Integer credits) {
        Page<Course> coursePage = courseService.pageCourses(current, size, courseName, credits);
        return Result.ok(coursePage);
    }

}    